using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.ConnectionEngine.Samples.Adapters;
using gen = System.Collections.Generic;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            if (Session["cnAdapter"] == null)
                Session["cnAdapter"] = GetNewAdapterInstance();

            SystemAdapterWS.SystemAdapter cnAdapter = (SystemAdapterWS.SystemAdapter)Session["cnAdapter"];

            int messagetypeid = Convert.ToInt32(Request["hidMessageType"]);
            string serviceproviderid = "100";
            SystemAdapterWS.KeyValueItem[] args = GetCollectionItems(messagetypeid);

            string interactivesessionurl = cnAdapter.GetInteractiveSessionInformation(serviceproviderid, messagetypeid, args);
            litInvocationURL.Text = String.Format(@"<div>Go to the <a href=""{0}"" target=""frmResult"">interactive session</a></div>", interactivesessionurl);
        }
    }

    private SystemAdapterWS.KeyValueItem[] GetCollectionItems(int messagetypeid)
    {
        switch (messagetypeid)
        {
            case 9023:
                return GetMessageType9023Arguments();
            case 9025:
                return GetMessageType9025Arguments();
        }
        return null;
    }

    private SystemAdapterWS.KeyValueItem[] GetMessageType9025Arguments()
    {
        gen.List<SystemAdapterWS.KeyValueItem> args = new gen.List<SystemAdapterWS.KeyValueItem>();
        args.Add(CreateKeyValueItem("PatientID",txtPatientID.Text));
        args.Add(CreateKeyValueItem("PracticionerID",txtPracticionerID.Text));
        args.Add(CreateKeyValueItem("TestGroup",txtLabTestType.Text));
        return args.ToArray();
    }

    private SystemAdapterWS.KeyValueItem[] GetMessageType9023Arguments()
    {
        gen.List<SystemAdapterWS.KeyValueItem> args = new gen.List<SystemAdapterWS.KeyValueItem>();
        args.Add(CreateKeyValueItem("FirstName", txtFirstName.Text));
        args.Add(CreateKeyValueItem("LastName", txtLastName.Text));
        args.Add(CreateKeyValueItem("NHI",txtNHI.Text));
        return args.ToArray();
    }

    private SystemAdapterWS.KeyValueItem CreateKeyValueItem(string itemkey, string itemvalue)
    {
        SystemAdapterWS.KeyValueItem item = new SystemAdapterWS.KeyValueItem();
        item.Key = itemkey;
        item.Value = itemvalue;
        return item;
    }


    private SystemAdapterWS.SystemAdapter GetNewAdapterInstance()
    {
        SystemAdapterWS.SystemAdapter cnAdapter = new SystemAdapterWS.SystemAdapter();
        return cnAdapter;
    }
}
